package com.spic.business.accident.interevents.classreport.service.impl;

import java.util.List;

import com.spic.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.spic.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.spic.business.accident.interevents.classreport.domain.CheckHiddenTroubleRectification;
import com.spic.business.accident.interevents.classreport.mapper.InternalEventInspectionReportMapper;
import com.spic.business.accident.interevents.classreport.domain.InternalEventInspectionReport;
import com.spic.business.accident.interevents.classreport.service.IInternalEventInspectionReportService;

/**
 * 内部事件班组自查报告Service业务层处理
 *
 * @author 任翔
 * @date 2021-12-01
 */
@Service
public class InternalEventInspectionReportServiceImpl implements IInternalEventInspectionReportService {
    @Autowired
    private InternalEventInspectionReportMapper internalEventInspectionReportMapper;

    /**
     * 查询内部事件班组自查报告
     *
     * @param id 内部事件班组自查报告主键
     * @return 内部事件班组自查报告
     */
    @Override
    public InternalEventInspectionReport selectInternalEventInspectionReportById(Long id) {
        return internalEventInspectionReportMapper.selectInternalEventInspectionReportById(id);
    }

    /**
     * 查询内部事件班组自查报告列表
     *
     * @param internalEventInspectionReport 内部事件班组自查报告
     * @return 内部事件班组自查报告
     */
    @Override
    public List<InternalEventInspectionReport> selectInternalEventInspectionReportList(InternalEventInspectionReport internalEventInspectionReport) {
        return internalEventInspectionReportMapper.selectInternalEventInspectionReportList(internalEventInspectionReport);
    }

    /**
     * 新增内部事件班组自查报告
     *
     * @param internalEventInspectionReport 内部事件班组自查报告
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInternalEventInspectionReport(InternalEventInspectionReport internalEventInspectionReport) {
        internalEventInspectionReport.setCreateTime(DateUtils.getNowDate());
        int rows = internalEventInspectionReportMapper.insertInternalEventInspectionReport(internalEventInspectionReport);
        insertCheckHiddenTroubleRectification(internalEventInspectionReport);
        return rows;
    }

    /**
     * 修改内部事件班组自查报告
     *
     * @param internalEventInspectionReport 内部事件班组自查报告
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInternalEventInspectionReport(InternalEventInspectionReport internalEventInspectionReport) {
        internalEventInspectionReport.setUpdateTime(DateUtils.getNowDate());
        internalEventInspectionReportMapper.deleteCheckHiddenTroubleRectificationByRectificationCheckId(internalEventInspectionReport.getId());
        insertCheckHiddenTroubleRectification(internalEventInspectionReport);
        return internalEventInspectionReportMapper.updateInternalEventInspectionReport(internalEventInspectionReport);
    }

    /**
     * 批量删除内部事件班组自查报告
     *
     * @param ids 需要删除的内部事件班组自查报告主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInternalEventInspectionReportByIds(Long[] ids) {
        internalEventInspectionReportMapper.deleteCheckHiddenTroubleRectificationByIds(ids);
        return internalEventInspectionReportMapper.deleteInternalEventInspectionReportByIds(ids);
    }

    /**
     * 删除内部事件班组自查报告信息
     *
     * @param id 内部事件班组自查报告主键
     * @return 结果
     */
    @Override
    public int deleteInternalEventInspectionReportById(Long id) {
        internalEventInspectionReportMapper.deleteCheckHiddenTroubleRectificationByRectificationCheckId(id);
        return internalEventInspectionReportMapper.deleteInternalEventInspectionReportById(id);
    }

    /**
     * 新增自查隐患整改信息
     *
     * @param internalEventInspectionReport 内部事件班组自查报告对象
     */
    public void insertCheckHiddenTroubleRectification(InternalEventInspectionReport internalEventInspectionReport) {
        List<CheckHiddenTroubleRectification> checkHiddenTroubleRectificationList = internalEventInspectionReport.getCheckHiddenTroubleRectificationList();
        Long id = internalEventInspectionReport.getId();
        if (StringUtils.isNotNull(checkHiddenTroubleRectificationList)) {
            List<CheckHiddenTroubleRectification> list = new ArrayList<CheckHiddenTroubleRectification>();
            for (CheckHiddenTroubleRectification checkHiddenTroubleRectification : checkHiddenTroubleRectificationList) {
                checkHiddenTroubleRectification.setRectificationCheckId(id);
                list.add(checkHiddenTroubleRectification);
            }
            if (list.size() > 0) {
                internalEventInspectionReportMapper.batchCheckHiddenTroubleRectification(list);
            }
        }
    }
}
